import Vue from 'vue'
import App from './App'
import store from './store'
import requestModule from '@/plugins/request'
import global from '@/plugins/global'
import uView from "uview-ui";
// 导入LeanCloud
import leanCloud from '@/service/leanCloud'

Vue.use(uView);

// 初始化LeanCloud
leanCloud.init();

uni.$u.config.unit = 'rpx'
const msg = (title, duration=1500, mask=false, icon='none')=>{
	//统一提示方便全局修改
	if(Boolean(title) === false){
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
const json = type=>{
	//模拟异步请求数据
	return new Promise(resolve=>{
		setTimeout(()=>{
			resolve(Json[type]);
		}, 500)
	})
}

const prePage = ()=>{
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

Vue.config.productionTip = false
Vue.prototype.$fire = new Vue();
Vue.prototype.$store = store;
Vue.prototype.$request = requestModule.request
Vue.prototype.$uploadRequest = requestModule.uploadRequest
Vue.prototype.$requestTest = requestModule.requestTest
Vue.prototype.$cancelRequest = requestModule.cancelRequest
Vue.prototype.$global = global;
// 将LeanCloud添加到Vue原型
Vue.prototype.$leanCloud = leanCloud;
Vue.prototype.$apiMsg = {msg, json, prePage};
App.mpType = 'app'

// 注册到 Vue 原型上
Vue.prototype.$request = requestModule.request
Vue.prototype.$uploadRequest = requestModule.uploadRequest
Vue.prototype.$requestTest = requestModule.requestTest
Vue.prototype.$cancelRequest = requestModule.cancelRequest

// 如果需要，也可以注册到 uni 对象上
uni.$cancelRequest = requestModule.cancelRequest

Vue.config.productionTip = false;


Vue.mixin({
	methods: {
		setData: function(obj, callback) {
			let that = this;
			let keys = [];
			let val, data;
			Object.keys(obj).forEach(function(key) {
				keys = key.split('.');
				val = obj[key];
				data = that.$data;
				keys.forEach(function(key2, index) {
					if (index + 1 == keys.length) {
						that.$set(data, key2, val);
					} else {
						if (!data[key2]) {
							that.$set(data, key2, {});
						}
					}
					data = data[key2];
				})
			});
			callback && callback();
		} 
	}
});

// 添加全局错误处理
const errorHandler = (err, vm, info) => {
	console.error('应用错误:', err);
	console.error('错误信息:', info);
	
	// 在开发环境下显示错误
	if (process.env.NODE_ENV === 'development') {
		uni.showModal({
			title: '应用错误',
			content: `${err.message || err}\n来源: ${info}`,
			showCancel: false
		});
	}
};

// 添加全局未捕获Promise错误处理
// 判断环境，只在非小程序环境添加window事件监听
// #ifndef MP-WEIXIN
if (typeof window !== 'undefined') {
	window.addEventListener('unhandledrejection', event => {
		console.error('未处理的Promise错误:', event.reason);
		
		// 在开发环境下显示错误
		if (process.env.NODE_ENV === 'development') {
			uni.showModal({
				title: '未处理的Promise错误',
				content: event.reason.message || String(event.reason),
				showCancel: false
			});
		}
	});
}
// #endif

Vue.config.errorHandler = errorHandler;
Vue.prototype.$throw = (err) => errorHandler(err, null, '手动抛出');

// 调试日志功能
const isDebug = process.env.NODE_ENV === 'development';
Vue.prototype.$log = (...args) => {
	if (isDebug) {
		console.log(...args);
	}
};

const app = new Vue({
	store,
	...App
})
app.$mount()
